import axios from 'axios';


const virtApi = axios.create({
  baseURL: process.env.NODE_ENV === "development" ? 'http://127.0.0.1:5000' : '', 
  timeout: 10000, 
});

// 获取所有虚拟机的信息
export const getAllVms = async () => {
  try {
    const response = await virtApi.get('/vms');
    return response.data;
  } catch (error) {
    console.error('获取所有虚拟机信息时出错:', error);
    throw error;
  }
};

// 获取指定虚拟机的状态
export const getVmState = async (vmName: string) => {
  try {
    const response = await virtApi.get(`/vms/${vmName}/state`);
    return response.data;
  } catch (error) {
    console.error('获取指定虚拟机状态时出错:', error);
    throw error;
  }
};

// 启动指定虚拟机
export const startVm = async (vmName: string) => {
  try {
    const response = await virtApi.post(`/vms/${vmName}/start`);
    return response.data;
  } catch (error) {
    console.error('启动指定虚拟机时出错:', error);
    throw error;
  }
};

// 暂停指定虚拟机
export const pauseVm = async (vmName: string) => {
  try {
    const response = await virtApi.post(`/vms/${vmName}/suspend`);
    return response.data;
  } catch (error) {
    console.error('暂停指定虚拟机时出错:', error);
    throw error;
  }
};

// 关闭指定虚拟机
export const shutdownVm = async (vmName: string) => {
  try {
    const response = await virtApi.post(`/vms/${vmName}/shutdown`);
    return response.data;
  } catch (error) {
    console.error('关闭指定虚拟机时出错:', error);
    throw error;
  }
};

// 删除指定虚拟机
export const deleteVm = async (vmName: string, disksToDelete: string[]) => {
    try {
        const response = await virtApi.post(`/vms/${vmName}/delete`, { disks: disksToDelete });
        return response.data;
    } catch (error) {
        console.error('删除指定虚拟机时出错:', error);
        throw error;
    }
};

// 强制关闭指定虚拟机
export const forceShutdownVm = async (vmName: string) => {
  try {
    const response = await virtApi.post(`/vms/${vmName}/force-shutdown`);
    return response.data;
  } catch (error) {
    console.error('强制关闭指定虚拟机时出错:', error);
    throw error;
  }
};

// 克隆虚拟机
export const cloneVm = async (sourceVmName: string, cloneVmName: string) => {
  try {
    const response = await virtApi.post('/vms/clone', {
      source_vm_name: sourceVmName,
      clone_vm_name: cloneVmName,
    });
    return response.data;
  } catch (error) {
    console.error('克隆虚拟机时出错:', error);
    throw error;
  }
};

// 根据提供的 XML 描述创建虚拟机
export const createVm = async (xmlDesc: string) => {
  try {
    const response = await virtApi.post('/vms/create', {
      xml_desc: xmlDesc,
    });
    return response.data;
  } catch (error) {
    console.error('创建虚拟机时出错:', error);
    throw error;
  }
};

// 获取虚拟机的图形端口
export const getVmGraphicsPort = async (vmName: string) => {
  try {
    const response = await virtApi.get('/token', {
      params: {
        token: vmName,
      },
    });
    return response.data;
  } catch (error) {
    console.error('获取虚拟机图形端口时出错:', error);
    throw error;
  }
};

// 获取 KVM 网络接口信息
export const getKvmNetworks = async () => {
  try {
    const response = await virtApi.get('/kvm/networks');
    return response.data;
  } catch (error) {
    console.error('获取 KVM 网络接口信息时出错:', error);
    throw error;
  }
};

// 获取 /libvirt/images 接口的镜像信息
export const getLibvirtImages = async () => {
  try {
    const response = await virtApi.get('/libvirt/images');
    return response.data;
  } catch (error) {
    console.error('获取 /libvirt/images 接口的镜像信息时出错:', error);
    throw error;
  }
};
